﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shared.Model;
using Client;
using ManageSystem.Dialog;

namespace ManageSystem.Manager
{
    public partial class FormLine : UserControl
    {
        List<LINE> _listLine = null;
        LINE _selectedLine=null;
        public FormLine()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 清空信息
        /// </summary>
        private void ClearText()
        {
            this.textBoxFlowId.Clear();
            this.textBoxLineId.Clear();
            this.textBoxLineName.Clear();
            this.textBoxLineContext.Clear();
            this.textBoxLineLeaderId.Clear();
            this.textBoxLineLeaderName.Clear();
        }
        /// <summary>
        /// 设置Text信息
        /// </summary>
        /// <param name="line"></param>
        private void SetText(LINE line)
        {
            if (line == null) return;

            this.textBoxLineId.Text = line.ID.ToString();
            this.textBoxLineName.Text = line.NAME;
            this.textBoxLineContext.Text = line.CONTEXT;
            if (line.FLOW_ID != null)
                this.textBoxFlowId.Text = line.FLOW_ID.ToString();
            if (line.LEADER_ID != null)
                this.textBoxLineLeaderId.Text = line.LEADER_ID.Value.ToString();
            this.textBoxLineLeaderName.Text = line.LEADER_NAME;
        }
        /// <summary>
        /// 设置List view
        /// </summary>
        /// <param name="view"></param>
        /// <param name="line"></param>
        private void SetListViewLine(ListView view,LINE line)
        {
            if (view == null || line == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = line.ID.ToString();
            item.SubItems.Add(line.NAME);
            item.SubItems.Add(line.CONTEXT);

            if (line.FLOW_ID != null)
                item.SubItems.Add(line.FLOW_ID.ToString());
            else
                item.SubItems.Add("");

            if (line.LEADER_ID != null)
                item.SubItems.Add(line.LEADER_ID.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(line.LEADER_NAME);

            view.Items.Add(item);
        }
        /// <summary>
        /// 清空list view
        /// </summary>
        /// <param name="view"></param>
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        /// <summary>
        /// 设置Line
        /// </summary>
        /// <param name="line"></param>
        private void SetLine(LINE line)
        {
            if (line == null) return;

            string name = this.textBoxLineName.Text;
            string context = this.textBoxLineContext.Text;
            string flowId = this.textBoxFlowId.Text;
            string leaderId = this.textBoxLineLeaderId.Text;
            string leaderName = this.textBoxLineLeaderName.Text;

            if (string.IsNullOrEmpty(name) == false)
            {
                line.NAME = name;
            }

            if (string.IsNullOrEmpty(context) == false)
            {
                line.CONTEXT = context;
            }

            long fId = 0;
            if(string.IsNullOrEmpty(flowId)==false&&long.TryParse(flowId,out fId))
            {
                line.FLOW_ID = fId;
            }

            long ld = 0;
            if (string.IsNullOrEmpty(leaderId) == false && long.TryParse(leaderId, out ld))
            {
                line.LEADER_ID = ld;
            }

            if (string.IsNullOrEmpty(leaderName) == false)
            {
                line.LEADER_NAME = leaderName;
            }
        }
        //加载产线信息
        private void FormLine_Load(object sender, EventArgs e)
        {
            ClearText();
            ClearListView(this.listViewLine);

            //获得List
            LINE lineW = new LINE();
            var selectLine = MethodWcf.SelectLineClient(lineW);
            if (selectLine.result)
            {
                _listLine = selectLine.value;
                for(int i = 0; i < _listLine.Count; i++)
                {
                    SetListViewLine(this.listViewLine, _listLine[i]);
                }
            }
        }
        /// <summary>
        /// 添加产线信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            LINE line = new LINE();
            line.SetUpdate();
            SetLine(line);

            if (line.GetListUpdate().Count == 0) return;
            
            //添加产线信息
            var insertLine = MethodWcf.InsertLineClient(line);
            if (insertLine.result)
            {
                MessageBox.Show("添加成功");
            }else
            {
                MessageBox.Show(string.Format("添加失败：{0}",insertLine.message));
            }
            FormLine_Load(null, null);
        }
        /// <summary>
        /// 修改产线信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonChg_Click(object sender, EventArgs e)
        {
            if (_selectedLine == null) return;

            _selectedLine.SetWhere();
            _selectedLine.ID = _selectedLine.ID;
            _selectedLine.SetUpdate();
            SetLine(_selectedLine);

            if (_selectedLine.GetListUpdate().Count == 0) return;

            //更新产线信息
            var updateLine = MethodWcf.UpdateLineClient(_selectedLine);
            if (updateLine.result)
            {
                MessageBox.Show("更新成功");
            }else
            {
                MessageBox.Show(string.Format("更新失败：{0}",updateLine.message));
            }

            FormLine_Load(null, null);
            _selectedLine = null;
        }
        //选择流程
        private void textBoxFlowId_Enter(object sender, EventArgs e)
        {
            DialogFlow flow = new DialogFlow(this.textBoxFlowId);
            flow.ShowDialog();
        }
        //选择组长
        private void textBoxLineLeaderId_Enter(object sender, EventArgs e)
        {
            DialogWorker work = new DialogWorker(this.textBoxLineLeaderId, this.textBoxLineLeaderName);
            work.ShowDialog();
        }
        //搜索信息
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            //①搜索满足条件的行
            //②将相关的行显示在窗口中
            ClearListView(this.listViewLine);//清空链表
            string searchText = this.textBoxSearch.Text.Trim();
            if (string.IsNullOrEmpty(searchText))//如果没有搜索条件则将所有的信息加载进来
            {
                for (int i = 0; i < _listLine.Count; i++)
                {
                    SetListViewLine(this.listViewLine, _listLine[i]);
                }
                return;
            }

            var listSearch = (from n in _listLine
                              where string.Format("{0}|{1}|{2}|{3}|{4}|{5}",
                              n.ID,
                              n.NAME,
                              n.CONTEXT,
                              n.FLOW_ID,
                              n.LEADER_ID,
                              n.LEADER_NAME)
                              .Contains(searchText)
                              select n).ToList();
            if (listSearch == null || listSearch.Count == 0) return;

            //给List View赋值
            for (int i = 0; i < listSearch.Count; i++)
            {
                SetListViewLine(this.listViewLine, listSearch[i]);
            }
        }
        //单击操作
        private void listViewLine_MouseClick(object sender, MouseEventArgs e)
        {
            ClearText();
            if (this._listLine.Count == 1)//只有一条选项
            {
                _selectedLine = _listLine[0];
                SetText(_selectedLine);
            }

            var selected = this.listViewLine.SelectedItems;
            if (this.listViewLine.Items.Count >= 1 && selected.Count == 1)//ITEM的数量大于1
            {
                _selectedLine = (from f in _listLine
                                 where f.ID == long.Parse(selected[0].Text)
                                 select f).FirstOrDefault();

                SetText(this._selectedLine);
            }
        }
    }
}
